package com.example.qxfw.hbqx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.qx.HeavyForecast;
import com.example.qxfw.common.entity.xt.SysConfig;
import com.example.qxfw.common.entity.zy.*;
import com.example.qxfw.common.util.DateUtil;
import com.example.qxfw.hbqx.service.HjybcpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@ApiIgnore
@Api(tags = "环保气象-环境预报产品")
@RestController
@CrossOrigin
@RequestMapping("/hjybcp")
public class HjybcpController {
    @Autowired
    private HjybcpService hjybcpService;

    @ApiOperation("根据参数类型查询系统配置")
    @GetMapping(value = "/selectByParamTypeOfSysConfig")
    public SysResult selectByParamTypeOfSysConfig(String paramtype){
        List<SysConfig> list = hjybcpService.selectByParamTypeOfSysConfig(paramtype);
        return SysResult.success(list);
    }

    @ApiOperation("根据观测时间段查询污染扩散等级预报")
    @GetMapping(value = "/selectByObservTimeOfAirpolConFcst")
    public SysResult selectByObservTimeOfAirpolConFcst(String timeStart, String timeEnd){
        Date observtimeStart = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(timeStart));
        Date datobservtimeEnde = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(timeEnd));
        List<AirpolConFcst> list = hjybcpService.selectByObservTimeOfAirpolConFcst(observtimeStart, datobservtimeEnde);
        return SysResult.success(list);
    }

    @ApiOperation("根据观测时间更新污染扩散等级预报")
    @PutMapping("/updateByObservTime")
    public SysResult updateByObservTime(
            String time, String level, Integer totalweight, String asseaa, String describe, String condition){
        Date observtime = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(time));
        int result = hjybcpService.updateByObservTime(observtime, level, totalweight, asseaa, describe, condition);
        return SysResult.success(result);

    }

    @ApiOperation("根据观测时间更新污染扩散等级预报")
    @PostMapping("/insertOfAirPolConForecast")
    public SysResult insertOfAirPolConForecast(
            String time, String level, Integer totalweight, String asseaa, String describe, String condition){
        Date observtime = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(time));
        int result = hjybcpService.insertOfAirPolConForecast(observtime, level, totalweight, asseaa, describe, condition);
        return SysResult.success(result);

    }

    @ApiOperation("根据名字和类型修改参数赋值")
    @PutMapping("/updateByNameAndTypeOfSysConfig")
    public SysResult updateByNameAndTypeOfSysConfig(String paramvalue, String paramname, String paramtype){
        int result = hjybcpService.updateByNameAndTypeOfSysConfig(paramvalue, paramname, paramtype);
        return SysResult.success(result);

    }

    @ApiOperation("根据日期类型id删除空气质量精细化预报主表数据")
    @DeleteMapping("/deleteByDateTypeIdOfAirpolFcst")
    public SysResult deleteByDateTypeIdOfAirpolFcst(String date, Integer forecasttype, String stationid){
        Date datechar = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(date));
        int result = hjybcpService.deleteByDateTypeIdOfAirpolFcst(datechar, forecasttype, stationid);
        return SysResult.success(result);

    }

    @ApiOperation("添加空气质量精细化预报结果表")
    @PostMapping("/insertOfAirPolFcstFinal")
    public SysResult insertOfAirPolFcstFinal(
            String stationid, String stationname, String date,
            Integer forecasttype, Integer ntimes, Double so2, Double no2,
            Double pm10, Double co, Double o3_1h, Double o3_8h, Double pm2_5, String pri,
            Integer aqi, Double longitude, Double latitude, Double altitude
    ) {
        Date datechar = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(date));
        int result = hjybcpService.insertOfAirPolFcstFinal(
                stationid, stationname, datechar, forecasttype, ntimes, so2,
                no2, pm10, co, o3_1h, o3_8h, pm2_5, pri, aqi, longitude, latitude, altitude
        );
        return SysResult.success(result);
    }

    @ApiOperation("根据日期和预报类型查询空气质量精细化预报主表")
    @GetMapping("/selectByDateAndTypeOfAirpolFcst")
    public SysResult selectByDateAndTypeOfAirpolFcst(String date, Integer forecasttype){
        Date datechar = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(date));
        List<AirpolFcst> list = hjybcpService.selectByDateAndTypeOfAirpolFcst(datechar, forecasttype);
        return SysResult.success(list);
    }

    @ApiOperation("查询所有环保站信息")
    @GetMapping("/selectOfEnviroStation")
    public SysResult selectOfEnviroStation(){
        List<EnviroStation> list = hjybcpService.selectOfEnviroStation();
        return SysResult.success(list);
    }

    @ApiOperation("根据站点id日期预报类型查询空气质量精细化预报主表")
    @GetMapping("/selectByIdDateTypeOfAirpolFcst")
    public SysResult selectByIdDateTypeOfAirpolFcst(
            String stationid, String dateStart, String dateEnd, Integer forecasttype){
        Date datecharStart = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(dateStart));
        Date datecharEnd = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(dateEnd));
        List<AirpolFcst> list = hjybcpService.selectByIdDateTypeOfAirpolFcst(stationid, datecharStart, datecharEnd, forecasttype);
        return SysResult.success(list);
    }

    @ApiOperation("根据日期查询空气质量精细化预报主表,不包含站点id的")
    @GetMapping("/selectByNotIdAndDateOfAirpolFcst")
    public SysResult selectByNotIdAndDateOfAirpolFcst(
            String stationid, String timeStart, String timeEnd){
        Date observtimeStart = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(timeStart));
        Date observtimeEnd = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(timeEnd));
        List<AirPollution> list = hjybcpService.selectByNotIdAndDateOfAirpolFcst(stationid, observtimeStart, observtimeEnd);
        return SysResult.success(list);
    }

    @ApiOperation("根据站点id日期根据站点id日期预报类型查询空气质量精细化预报结果表预报类型查询空气质量精细化预报主表")
    @GetMapping("/selectByIdDateTypeOfAirPolFcstFinal")
    public SysResult selectByIdDateTypeOfAirPolFcstFinal(
            String stationid, String dateStart, String dateEnd, Integer forecasttype){
        Date datecharStart = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(dateStart));
        Date datecharEnd = DateUtil.localDateTimeToDate(DateUtil.stringToLocalDateTime(dateEnd));
        List<AirPolFcstFinal> list =
                hjybcpService.selectByIdDateTypeOfAirPolFcstFinal(
                        stationid, datecharStart, datecharEnd, forecasttype);
        return SysResult.success(list);
    }

    @ApiOperation("更新或插入重要污染预报")
    @PostMapping("/selectByNowTimeAndjudgetypeOfHeavyForecast")
    public SysResult selectByNowTimeAndjudgetypeOfHeavyForecast(HeavyForecast heavyForecast){
        if (heavyForecast == null){
            return SysResult.fail("参数为空！");
        }
        List<HeavyForecast> list = hjybcpService.selectByNowTimeAndjudgetypeOfHeavyForecast(heavyForecast);
        if (list.size() > 0){
            int x = hjybcpService.updateOfHeavyForecast(heavyForecast);
            return SysResult.success("更新数据成功",x,heavyForecast);
        }else {
            int y = hjybcpService.insertOfHeavyForecast(heavyForecast);
            return SysResult.success("添加数据成功",y,heavyForecast);
        }
    }

    @ApiOperation("根据日期类型查询点查询空气质量精细化预报主表")
    @GetMapping("/selectByDateAndTimeAndTypeOfAirpolFcst")
    public SysResult selectByDateAndTimeAndTypeOfAirpolFcst(
            String date, String time, Integer forecasttype){
        StringBuilder builder = new StringBuilder();
        builder.append(date);
        builder.append(" ");
        builder.append(time);
        Timestamp datechar = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(builder.toString()));
        List<AirpolFcst> list = hjybcpService.selectByDateAndTimeAndTypeOfAirpolFcst(datechar, forecasttype);
        return SysResult.success(list);
    }


/*
    @ApiOperation("自动判断（AQI模型）")
*/




}
